package com.hibase.core.web.exception;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.hibase.common.constant.ResponseCode;
import com.hibase.common.entity.ResponseModel;
import com.hibase.common.exception.HibaseExcelException;
import com.hibase.common.exception.HibaseException;
import com.hibase.core.dingding.util.DingDingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import java.util.List;

/**
 * 异常处理器
 *
 * @Author scott
 * @Date 2019
 */
@RestControllerAdvice
@Slf4j
public class HibaseExceptionHandler {


    /**
     * spring默认上传大小100MB 超出大小捕获异常MaxUploadSizeExceededException
     */
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ResponseModel<?> handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e) {
        log.error(ExceptionUtil.stacktraceToString(e), e);
        return ResponseModel.failMsg("文件大小超出限制, 请压缩或降低文件质量! ");
    }

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(HibaseException.class)
    public ResponseModel<?> handleHibaseException(HibaseException e) {
        log.error(ExceptionUtil.stacktraceToString(e), e);
        return ResponseModel.failMsg(e.getCode(), e.getMessage());
    }

    /**
     * Excel 异常处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(HibaseExcelException.class)
    public ResponseModel<?> handleHibaseExcelException(HibaseExcelException e) {
        log.error(ExceptionUtil.stacktraceToString(e), e);
        return ResponseModel.failMsg(e.getCode(), ExceptionUtil.stacktraceToString(e), e.getMessage());
    }

    @ExceptionHandler({Exception.class})
    public ResponseModel<?> handleException(Exception e) {
        log.error(ExceptionUtil.stacktraceToString(e), e);
//        StackTraceElement ste =e.getStackTrace()[0];
//        return ResponseModel.failMsg(ResponseCode.SERVER_ERROR.getCode(),"系统错误,错误信息如下:"
//                +ste.getClassName()+"."
//                +ste.getMethodName() +",行数:"
//                +ste.getLineNumber() + ",异常信息:"
//                +e.toString()
//        );
        DingDingUtil.sendExceptionMessage(ExceptionUtil.stacktraceToString(e, 500));
        return ResponseModel.failMsg(ResponseCode.SERVER_ERROR.getCode(), ExceptionUtil.getRootCauseMessage(e), ResponseCode.SERVER_ERROR.getMessage());
    }

    /**
     * validation Exception
     *
     * @param exception
     * @return R
     */
    @ExceptionHandler({MethodArgumentNotValidException.class})
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResponseModel<?> handleBodyValidException(MethodArgumentNotValidException exception) {
        List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors();
        log.warn("参数绑定异常,ex = {}", fieldErrors.get(0).getDefaultMessage());
        return ResponseModel.failMsg(fieldErrors.get(0).getDefaultMessage());
    }

}
